LoadPersons.java
package com.ivoronline.springboot_db_query_native.runners;
import com.ivoronline.springboot_db_query_native.entities.Person;
import com.ivoronline.springboot_db_query_native.repositories.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
@Component
@Order(1)
public class LoadPersons implements CommandLineRunner {
@Autowired PersonRepository personRepository;
@Override
@Transactional
public void run(String... args) throws Exception {
personRepository.save(new Person("John" , 20));
personRepository.save(new Person("John" , 21));
personRepository.save(new Person("Bill" , 30));
personRepository.save(new Person("Nancy", 40));
personRepository.save(new Person("Susan", 50));
}
}
MyController.java
package com.ivoronline.springboot_db_query_native.controllers;
import com.ivoronline.springboot_db_query_native.entities.Person;
import com.ivoronline.springboot_db_query_native.repositories.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
public class MyController {
@Autowired PersonRepository personRepository;
//================================================================
// SELECT JOHN
//================================================================
@ResponseBody
@RequestMapping("/SelectJohn")
public Person selectJohn() {
Person john = personRepository.getJohn();
return john;
}
//================================================================
// SELECT PERSON BY NAME AGE INDEXED
//================================================================
@ResponseBody
@RequestMapping("/SelectPersonByNameAgeIndexed")